﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;

public partial class Bill : System.Web.UI.Page
{
    private int foodID = 0;
    private Dictionary<Int32, Int32> cart;
    private Dictionary<Int32, String> cartImage;
    private DataTable allFoodTable;

    protected void Page_Load(object sender, EventArgs e)
    {
        //String temp = Request.QueryString["RefreshPage"];
        //Response.Write(Request.QueryString["RefreshPage"]);
        if (Request.QueryString["FoodID"] != null)
        {
            foodID = Int32.Parse(Request.QueryString["FoodID"].ToString());

            cart = (Dictionary<Int32, Int32>)Session["Cart"];
            cartImage = (Dictionary<Int32, String>)Session["CartImage"];
            allFoodTable = ((System.Data.DataView)(Session["FoodListAll"])).Table;

            if (cart.ContainsKey(foodID))
            {
                cart[foodID]++;
            }
            else
            {
                cart.Add(foodID, 1);
                cartImage.Add(foodID, Request.QueryString["FoodImage"].ToString());
            }

            ShowCheckout();
        }
        else if (Request.QueryString["RefreshPage"] == "true")
        {
            cart = (Dictionary<Int32, Int32>)Session["Cart"];
            cartImage = (Dictionary<Int32, String>)Session["CartImage"];
            allFoodTable = ((System.Data.DataView)(Session["FoodListAll"])).Table;
            
            ShowCheckout();
        }

        
    }

    private void ShowCheckout()
    {
        if (cart.Count() > 0)
        {
            ShowBill();
            butCheckout.Visible = true;
        }
        else
        {
            butCheckout.Visible = false;
        }
    }

    private void ShowBill()
    {
        int sumPrice = 0;
        foreach (KeyValuePair<Int32,Int32> pair in cart)
        {
            DataRow[] result = allFoodTable.Select("FoodID = '" + pair.Key + "'");
            sumPrice += Int32.Parse(result[0]["Price"].ToString()) * pair.Value;

            HtmlTableRow row1 = new HtmlTableRow();
            

            HtmlTableCell cell1_2 = new HtmlTableCell();
            Label lb2 = new Label();
            lb2.Text = "<input type='number' disabled='true' style='width:35px' value='" + pair.Value.ToString() + "'>";
            cell1_2.Controls.Add(lb2);
            row1.Controls.Add(cell1_2);

            HtmlTableCell cell1_1 = new HtmlTableCell();
            Image img1 = new Image();
            img1.ImageUrl = cartImage[pair.Key];
            img1.Width = 70;
            img1.Height = 70;
            if (pair.Key == foodID)
            {
                img1.CssClass = "glow-soft";
            }
            cell1_1.Controls.Add(img1);
            row1.Controls.Add(cell1_1);

            mainTable.Controls.Add(row1);
        }

        HtmlTableRow row2 = new HtmlTableRow();

        HtmlTableCell cell2_2 = new HtmlTableCell();
        Label lb3 = new Label();
        lb3.Text = "Total:";
        cell2_2.Controls.Add(lb3);
        row2.Controls.Add(cell2_2);

        HtmlTableCell cell2_1 = new HtmlTableCell();
        Label lbSum = new Label();
        lbSum.Text = "<strong>" + formatPrice(sumPrice) + "</strong>";
        cell2_1.Controls.Add(lbSum);
        row2.Controls.Add(cell2_1);
        mainTable.Controls.Add(row2);
    }

    private string formatPrice(int price)
    {
        string stringPrice = price.ToString();
        string moneyUnit = " Đ";
        if (price < 1000)
        {
            return stringPrice + moneyUnit;
        }
        else if (price < 1000000)
        {
            return stringPrice.Substring(0, stringPrice.Length - 3) + "." + stringPrice.Substring(stringPrice.Length - 3) + moneyUnit;
        }
        else if (price < 1000000000)
        {
            return stringPrice.Substring(0, stringPrice.Length - 6) + "." + stringPrice.Substring(stringPrice.Length - 5, stringPrice.Length - 4) + "." + stringPrice.Substring(stringPrice.Length - 3) + moneyUnit;
        }
        else
        {
            return price + moneyUnit;
        }
    }
}